package 剑指offer;

public class _11旋转数组中的最小数字 {
    public int minArray(int[] numbers) {
        if(numbers == null) {
            return 0;
        }
        if(numbers.length == 1) {
            return numbers[0];
        }
        int i = 0;
        int j = numbers.length-1;
        int mid = i;
        while(numbers[i] >= numbers[j]) {
            if(j - i == 1) {
                mid = j;
                break;
            }
            mid = (i+j) / 2;
            //这里是，如果他们三个都相同，就没法确定mid的位置了，要用线性探测去找
            if(numbers[mid] == numbers[i] && numbers[mid] == numbers[j]) {
                int idx = numbers[i];
                for(int k = i;k < j; k++) {
                    idx = idx < numbers[k] ? idx : numbers[k];
                }
                return idx;
            }
            if(numbers[mid] >= numbers[i]) {
                i = mid;
            }else if(numbers[mid] <= numbers[j]) {
                j = mid;
            }
        }
        return numbers[mid];
    }
}
